//
//  DXTextViewToolBar.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/14.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit
enum TextViewToolBarType:Int {
    case TextViewToolBarTypePicture = 0
    case TextViewToolBarTypeMention = 1
    case TextViewToolBarTypeTrend = 2
    case TextViewToolBarTypeEmoticon = 3
    case TextViewToolBarTypeAdd = 4
}
class DXTextViewToolBar: UIStackView {
    
    var toolBarButtonClickClosure:((Typ:TextViewToolBarType)->())?
    
    var emotionButton:UIButton?
    
//    设置button被点击后状态,该表button的图片
    var isClickEmotonKeyBoard:Bool = false{
        didSet{
            
            let emotionIMGNor:UIImage
            let emotionIMGHig:UIImage
            
            if isClickEmotonKeyBoard {

            emotionIMGNor = UIImage(named: "compose_keyboardbutton_background")!
            emotionIMGHig = UIImage(named: "compose_keyboardbutton_background_highlighted")!
            }else{
            emotionIMGNor = UIImage(named: "compose_emoticonbutton_background")!
            emotionIMGHig = UIImage(named: "compose_emoticonbutton_background_highlighted")!
            }
        emotionButton?.setImage(emotionIMGNor, forState: .Normal)
         emotionButton?.setImage(emotionIMGHig, forState: .Highlighted)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        axis = .Horizontal
//        spacing = 10
        distribution = .FillEqually
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 初始化UI
    private func setUpUI(){
    
        addToolBarButton("compose_toolbar_picture", type: .TextViewToolBarTypePicture)
        
        addToolBarButton("compose_mentionbutton_background", type: .TextViewToolBarTypeMention)

        addToolBarButton("compose_trendbutton_background", type: .TextViewToolBarTypeTrend)

       emotionButton = addToolBarButton("compose_emoticonbutton_background", type: .TextViewToolBarTypeEmoticon)

        addToolBarButton("compose_add_background", type: .TextViewToolBarTypeAdd)

    }
    // MARK: - 添加button
    private func addToolBarButton(imgName:String ,type:TextViewToolBarType)->(UIButton){
        let button = UIButton()
        button.setImage(UIImage(named: imgName), forState: .Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
        button.tag = type.rawValue
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        button.addTarget(self, action: "ToolBarButtonClick:", forControlEvents: .TouchUpInside)
    
        addArrangedSubview(button)
        
        return button
    }
    
    // MARK: - ToolBarButton点击事件
    @objc private func ToolBarButtonClick(sender:UIButton){
    
    toolBarButtonClickClosure?(Typ: TextViewToolBarType.init(rawValue: sender.tag)!)
    
    }
    
    
    
}
